package com.chainyoung.component_tmperson.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.entity.WalletFinancing;
import com.chainyoung.component_tmperson.ui.activity.financing.WalletFinancingProfitActivity;
import com.chainyoung.component_tmperson.ui.activity.financing.WalletFinancingRuleActivity;
import com.chainyoung.component_tmperson.ui.activity.financing.WalletFinancingTradeActivity;

import java.math.RoundingMode;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class WalletFinancingManageAdapter extends BaseQuickAdapter<WalletFinancing, BaseViewHolder> {


    private ImageLoader imageLoader;

    public WalletFinancingManageAdapter(@Nullable List<WalletFinancing> data) {
        super(R.layout.layout_wallet_financing_own_list_item, data);
        imageLoader = Utils.getAppComponent().imageLoader();
    }

    @Override
    protected void convert(BaseViewHolder helper, final WalletFinancing item) {

        imageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(item.getImg())
                        .imagerView((ImageView) (helper.getView(R.id.iv_logo)))
                        .build());

        helper.setText(R.id.tv_coin_name, item.getName().toUpperCase());
        helper.setText(R.id.tv_year_rate, item.getMax_rate().setScale(2, RoundingMode.DOWN) + "%");
        if (item.isIs_turn_in()) {
            helper.setText(R.id.tv_profit, StringUtil.subZeroAndDot(item.getUser_cumulative_profit().setScale(6, RoundingMode.DOWN) + ""));
        }
//        if (!item.isIs_turn_in()) {
//            helper.setText(R.id.tv_min_buy_amout, StringUtil.subZeroAndDot(item.getMin_in()) + " " + item.getName().toUpperCase() + "起投");
//        }
        helper.setText(R.id.tv_min_buy_amout, StringUtil.subZeroAndDot(item.getMin_in()) + " " + item.getName().toUpperCase() + "起投");

        if (item.isHas_current()) {
            helper.setText(R.id.tv_time, item.getSettle_time() + ":00(GMT+8)结算");
        }
        helper.getView(R.id.tv_time).setVisibility(item.isHas_current() ? View.VISIBLE : View.GONE);

        helper.getView(R.id.rl_financing).setBackgroundResource(item.isIs_turn_in() ? item.isIs_join()?R.mipmap.ic_already_buy_bg : R.drawable.shape_financing_bg_white: R.drawable.shape_financing_bg_white);
        helper.getView(R.id.tv_open).setVisibility(item.isIs_turn_in() ?item.isIs_join()? View.GONE : View.VISIBLE:View.VISIBLE);
       // helper.getView(R.id.tv_rules).setVisibility(item.isIs_turn_in() ? View.GONE : View.VISIBLE);
        //helper.getView(R.id.tv_min_buy_amout).setVisibility(item.isIs_turn_in() ? View.GONE : View.VISIBLE);
        helper.getView(R.id.tv_profit).setVisibility(item.isIs_turn_in() ? item.isIs_join()?View.VISIBLE:View.GONE : View.GONE);
        helper.getView(R.id.txt_profit).setVisibility(item.isIs_turn_in() ? item.isIs_join()?View.VISIBLE:View.GONE : View.GONE);

        helper.getView(R.id.rl_financing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isIs_turn_in()) {
                    Intent intent = new Intent(mContext, WalletFinancingProfitActivity.class);
                    intent.putExtra("walletFinancing", item);
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, WalletFinancingTradeActivity.class);
                    intent.putExtra("walletFinancing", item);
                    intent.putExtra("isFirstTrade",true);
                    intent.putExtra("type", "all");
                    mContext.startActivity(intent);
                }
            }
        });

        helper.getView(R.id.tv_rules).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(mContext, WalletFinancingRuleActivity.class);
                intent1.putExtra(URL, item.getArticle());
                intent1.putExtra("title", "规则");
                mContext.startActivity(intent1);
            }
        });


    }


}
